/**
 * 
 */
package com.lanswon.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanswon.common.util.StringUtil;
import com.lanswon.dao.ArticleMapper;
import com.lanswon.dao.CommonProblemMapper;
import com.lanswon.dao.SearchHotMapper;
import com.lanswon.entity.ArticleVO;
import com.lanswon.entity.CommonProblem;
import com.lanswon.entity.SearchHotVO;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: SearchService.java
 * @Description: 该类的功能描述
 *
 * @version: v1.0.0
 * @author: zm
 * @date: 2018年5月9日 上午9:53:56
 *
 */
@Service
public class SearchService {
	@Autowired
	private SearchHotMapper searchHotMapper;

	@Autowired
	private ArticleMapper articleMapper;

	@Autowired
	private CommonProblemMapper commonProblemMapper;

	/**
	 * @param searchContent
	 * @return
	 */
	public Map<String, Object> count(String searchContent) {

		List<SearchHotVO> hotVOs = new ArrayList<>();
		if (!StringUtil.isEmpty(searchContent)) {
			hotVOs = searchHotMapper.searchLike(searchContent);
			if (hotVOs != null && hotVOs.size() != 0) {
				// 修改已有记录
				for (SearchHotVO searchHotVO : hotVOs) {
					SearchHotVO update = new SearchHotVO();
					update.setSearchId(searchHotVO.getSearchId());
					update.setSearchDate(new Date());
					// 当前时间
					Long now = System.currentTimeMillis();
					// 设置数据库最后一次查询时间
					Long searchDate = searchHotVO.getSearchDate().getTime();
					// 当月加1，否则重置
					if ((now - searchDate) / (24 * 60 * 60 * 1000) <= 30) {
						update.setSearchCount(searchHotVO.getSearchCount() + 1);
					} else {
						update.setSearchCount(1L);
					}
					// 历史次数
					update.setSearchHistory(searchHotVO.getSearchHistory() + 1);
					searchHotMapper.updateByPrimaryKeySelective(update);
					// 更新热搜排序
					changeStatus();
				}
			} else {
				// 添加纪录
				SearchHotVO save = new SearchHotVO();
				save.setSearchContent(searchContent);
				save.setSearchCount(1L);
				save.setSearchDate(new Date());
				save.setSearchHistory(1L);
				save.setSearchStatus("1");
				searchHotMapper.insert(save);
			}
		}

		return getHotStatus();
	}

	public void changeStatus() {
		List<SearchHotVO> hotVOs = searchHotMapper.getOrder();
		if (hotVOs != null) {
			// 更新之前热搜
			searchHotMapper.backStatus();
			// 修改热搜状态
			for (SearchHotVO searchHotVO : hotVOs) {
				SearchHotVO update = new SearchHotVO();
				update.setSearchId(searchHotVO.getSearchId());
				update.setSearchStatus("2");
				searchHotMapper.updateByPrimaryKeySelective(update);
			}
		}

	}

	public Map<String, Object> getHotStatus() {
		// 设置响应信息
		String success = "";
		List<String> list = new ArrayList<>();

		List<SearchHotVO> hotVOs = searchHotMapper.getOrder();
		if (hotVOs != null) {
			success = "success";
			for (SearchHotVO searchHotVO : hotVOs) {
				list.add(searchHotVO.getSearchContent());
			}
		}
		// 返回信息
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		map.put("message", list);
		return map;
	}

	/**
	 * @param page
	 * @param limit
	 * @param content
	 * @param type
	 * @return
	 */
	public Object load(Integer page, Integer limit, String type, String content) {

		if (page == null) {
			page = 1;
		}
		int pageindex = (page - 1) * limit;

		List<SearchHotVO> searchHotVOs = new ArrayList<SearchHotVO>();
		int count;
		if (!StringUtil.isEmpty(type) && !StringUtil.isEmpty(content)) {
			searchHotVOs = searchHotMapper.loadByCondition(pageindex, limit, type, content);
			count = searchHotMapper.countByCondition(type, content);
		} else {
			searchHotVOs = searchHotMapper.load(pageindex, limit);
			count = searchHotMapper.count();
		}
		// 处理数据

		JSONArray jsonArray = new JSONArray();
		for (SearchHotVO searchHotVO : searchHotVOs) {
			Map map = new HashMap();
			map.put("searchId", searchHotVO.getSearchId());
			map.put("searchContent", searchHotVO.getSearchContent());
			map.put("searchCount", searchHotVO.getSearchCount());
			map.put("searchDate", new SimpleDateFormat("yyyy-MM-dd").format(searchHotVO.getSearchDate()));
			map.put("searchHistory", searchHotVO.getSearchHistory());
			map.put("searchStatus", searchHotVO.getSearchStatus());
			JSONObject json = JSONObject.fromObject(map);
			jsonArray.add(json);
		}
		// 组装数据
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", 0);
		jsonObject.put("msg", "");
		jsonObject.put("count", count);
		jsonObject.put("data", jsonArray);

		return jsonObject;
	}

	/**
	 * @param searchId
	 * @return
	 */
	public void reset(Long searchId) {
		SearchHotVO searchHotVO = new SearchHotVO();
		searchHotVO.setSearchId(searchId);
		searchHotVO.setSearchCount(0L);
		searchHotMapper.updateByPrimaryKeySelective(searchHotVO);
	}

	/**
	 * @param search
	 * @return
	 */
	public Map<String, Object> search(String search) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(StringUtil.isEmpty(search)) {
			resultMap.put("code", 1);
			resultMap.put("message", "搜索不到您输入的信息");
			return resultMap;
		}
		Map<String, Object> dataMap = new HashMap<String, Object>();

		List<ArticleVO> articleVOs = articleMapper.selectLike(search);
		List<CommonProblem> commonProblems = commonProblemMapper.selectLike(search);
		
		if (articleVOs != null && articleVOs.size() > 0) {
			dataMap.put("articleVOs", articleVOs);
		}
		if (commonProblems != null && commonProblems.size() > 0) {
			dataMap.put("commonProblems", commonProblems);
		}
		
		if(dataMap.size()!=0) {
			resultMap.put("code", 0);
			resultMap.put("data", dataMap);
		}else {
			resultMap.put("code", 1);
			resultMap.put("message", "搜索不到您输入的信息");
		}
		return resultMap;
	}

}
